<template>
  <!-- 用户信息 -->
  <div class="user-container myCenter my-animation-hideToShow">
    <!-- 背景图片 -->
    <el-image class="my-el-image" style="position: absolute" v-once lazy
      :src="$constant.two_poem_image[Math.floor(Math.random() * $constant.two_poem_image.length)]" fit="cover">
      <div slot="error" class="image-slot"></div>
    </el-image>
    <div class="shadow-box-mini user-info" style="display: flex">
      <div class="user-left">
        <div>
          <el-avatar class="user-avatar" @click.native="changeDialog" :size="60" :src="currentUser.user_pic"></el-avatar>
        </div>
        <div class="myCenter" style="margin-top: 12px">
          <div class="user-title">
            <div>昵称：</div>
            <div>邮箱：</div>
            <div>性别：</div>
            <div>简介：</div>
          </div>
          <div class="user-content">
            <div>
              <el-input autocomplete="new-password" maxlength="30" v-model="currentUser.nickname"></el-input>
            </div>
            <div>
              <el-input autocomplete="new-password" maxlength="30" v-model="currentUser.email"></el-input>
            </div>
            <div>
              <el-radio-group v-model="currentUser.gender">
                <el-radio :label="0" style="margin-right: 10px">薛定谔的猫</el-radio>
                <el-radio :label="1" style="margin-right: 10px">男</el-radio>
                <el-radio :label="2">女</el-radio>
              </el-radio-group>
            </div>
            <div>
              <el-input v-model="currentUser.introduction" maxlength="60" type="textarea" show-word-limit></el-input>
            </div>
          </div>
        </div>
        <div style="margin-top: 20px">
          <proButton :info="'提交'" @click.native="submitUserInfo()" :before="$constant.before_color_2"
            :after="$constant.after_color_2">
          </proButton>
        </div>
      </div>
      <div class="user-right">

      </div>
    </div>
  </div>

  <el-dialog title="上传头像" v-model="showDialog" width="30%" :before-close="clearDialog" :append-to-body="true"
    :close-on-click-modal="false" center>
    <div class="myCenter" style="flex-direction: column">
      <div>
        <uploadPicture :prefix="'userAvatar'" @addPicture="addPicture" :maxSize="1" :maxNumber="1"></uploadPicture>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { reactive, toRefs, onMounted } from 'vue'
import proButton from "@/components/common/proButton.vue"
import { ElMessage, ElMessageBox } from 'element-plus'
import { useStore } from 'vuex'
import { editUserInfo, userInfo, editAvatar } from "@/api/user"
import common from "@/utils/common"
import uploadPicture from "../../components/upload/uploadPicture.vue"
export default {
  name: 'User',
  components: { proButton, uploadPicture },
  setup() {
    const store = useStore();
    const data = reactive({
      user_id: "",
      currentUser: {
        user_pic: "",
        email: "",
        gender: null,
        nickname: "",
        introduction: ""
      },
      showDialog: false
    })
    const checkParameters = () => {
      if (common.isEmpty(data.currentUser.gender)) {
        ElMessage.error("请选择性别！");
        return false;
      }
      if (data.currentUser.nickname.indexOf(" ") !== -1) {
        ElMessage.error("昵称不能包含空格！");
        return false;
      }
      return true;
    }
    const getUserInfo = () => {
      userInfo().then((res) => {
        data.currentUser = res.data
        data.user_id = res.data.id
      }).catch(error => {

      })
    }
    const changeDialog = () => {
      if (common.isEmpty(data.currentUser.email)) {
        ElMessage.error("请输入邮箱！")
        return false;
      }
      if (!(/^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/.test(data.currentUser.email))) {
        ElMessage.error("邮箱格式有误！")
        return false;
      }
      data.showDialog = true;
    }
    const submitUserInfo = () => {
      if (!checkParameters()) {
        return;
      }
      ElMessageBox.confirm('确认保存？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'success',
        center: true
      }).then(() => {
        editUserInfo(data.currentUser)
          .then((res) => {
            store.commit('set_user', data.currentUser);
            ElMessage.success('修改成功！!');
    
            getUserInfo()
          })
          .catch((error) => {

          });
      }).catch(() => {
        ElMessage.info('已取消保存');
      });
    }

    const submitDialog = () => {
      if (common.isEmpty(data.currentUser.user_pic)) {
        ElMessage.error('请上传头像！');
      }
    }
    const addPicture = (value) => {
      data.currentUser.user_pic = value;
      submitDialog()
      clearDialog()
    }
    const clearDialog = () => {
      data.showDialog = false
    }
    onMounted(() => {
      getUserInfo()
    })
    return {
      ...toRefs(data),
      submitUserInfo,
      changeDialog,
      submitDialog,
      addPicture,
      clearDialog
    }
  },
}

</script>
<style scoped>
.user-container {
  width: 100vw;
  height: 100vh;
  position: relative;
}

.user-info {
  width: 80%;
  z-index: 10;
  margin-top: 70px;
  height: calc(100vh - 90px);
  margin-bottom: 20px;
  border-radius: 10px;
  overflow: hidden;
}

.user-left {
  width: 50%;
  background: var(--maxMaxWhiteMask);
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow-y: auto;
  padding: 20px;
}

.user-right {
  width: 50%;
  background: var(--maxWhiteMask);
  padding: 20px;
}

.user-title {
  text-align: right;
  user-select: none;
}

.user-content {
  text-align: left;
}

.user-title div {
  height: 55px;
  line-height: 55px;
  text-align: center;
}

.user-content>div {
  height: 55px;
  display: flex;
  align-items: center;
}

.user-content>>>.el-input__wrapper,
.user-content>>>.el-input__wrapper.is-focus,
.user-content>>>.el-textarea__inner {
  border: none !important;
  background: var(--whiteMask) !important;
  box-shadow: 0 0 0 0px var(--el-input-border-color, var(--el-border-color)) inset;
}

.user-content>>>.el-input__count {
  background: var(--transparent);
  user-select: none;
}

.changeInfo {
  color: var(--white);
  font-size: 0.75rem;
  cursor: pointer;
  background: var(--themeBackground);
  padding: 3px;
  border-radius: 0.2rem;
  user-select: none;
}

@media screen and (max-width: 920px) {
  .user-info {
    width: 90%;
  }

  .user-left {
    width: 100%;
  }

  .user-right {
    display: none;
  }
}
</style>